
    Private Sub BT_ExportGant_Click(sender As Object, e As RoutedEventArgs)
        Dim bmp As RenderTargetBitmap
        Dim bitmapEncoder As BitmapEncoder
        Dim width As Integer




        'Extract the GanttChart region from GanttControl.
        Dim chart As ScrollViewer = GanttControl.DependencyObjectExtensions.FindName(Of ScrollViewer)(GanttComm, "PART_ScheduleViewScrollViewer")
        If IsNothing(chart) Then
            MsgBox("non funzionante...")
            Exit Sub
        End If
        If RB_TuttoGant.IsChecked Then
            'Measure the total size of the Gantt Rendered. 
            GanttComm.Measure(New System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity))
            GanttComm.Arrange(New Rect(New System.Windows.Size(GanttComm.DesiredSize.Width, GanttComm.DesiredSize.Height)))
            bmp = New RenderTargetBitmap(CInt(GanttComm.DesiredSize.Width), CInt(GanttComm.DesiredSize.Height), 96, 96, PixelFormats.[Default])
        Else
            bmp = New RenderTargetBitmap(CInt(GanttComm.ActualWidth), CInt(GanttComm.ActualHeight), 96, 96, PixelFormats.[Default])
        End If



        
        Dim StackStampa As New StackPanel
        StackStampa.Margin = New Thickness(15)

        Dim Comm As Commesse = CB_Commesse.SelectedItem
        Dim Commessa As GESTIONE_COMMESSEDataSet.Commesse_TecnelRow = Comm.CommRow 'ComboCommesse.SelectedItem.row
        Dim Testo As New TextBlock : Testo.Text = "Programma lavori per " & Commessa.Sigla_Commessa_Tecnel & "/" & Commessa.Sigla_Commessa_Cliente
        Testo.FontSize = 20 : Testo.Margin = New Thickness(20)

        GanttComm.HorizontalAlignment = Windows.HorizontalAlignment.Left
        GridGant.Children.Remove(GanttComm)
        StackStampa.Children.Add(Testo)
        Dim Misuretemp As New System.Windows.Size(GanttComm.DesiredSize.Width, GanttComm.DesiredSize.Height * 1.1 + Testo.DesiredSize.Height) 'System.Windows.Size(30000, GanttComm.DesiredSize.Height + Testo.DesiredSize.Height)

        StackStampa.Arrange(New Rect(Misuretemp))

        StackStampa.Children.Add(GanttComm)

        bmp.Render(StackStampa)


        bitmapEncoder = New PngBitmapEncoder()

        bitmapEncoder.Frames.Add(BitmapFrame.Create(bmp))

        Dim saveFileDialog As New Microsoft.Win32.SaveFileDialog()

        saveFileDialog.DefaultExt = "png"
        saveFileDialog.Filter = "PNG Files (*.png)|*.png|Jpeg Files (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp|All files (*.*)|*.*"
        saveFileDialog.FilterIndex = 1

        If saveFileDialog.ShowDialog() = True Then
            Using stream As Stream = saveFileDialog.OpenFile()
                bitmapEncoder.Save(stream)
                stream.Close()
                System.Diagnostics.Process.Start(saveFileDialog.FileName)
            End Using
        End If
        StackStampa.Children.Remove(GanttComm)
        GridGant.Children.Add(GanttComm)
    End Sub